package org.example.completed.controller;


import org.example.completed.domain.Active;
import org.example.completed.domain.ActivityReservation;
import org.example.completed.domain.Reservation;
import org.example.completed.domain.User;
import org.example.completed.mapper.ActivityReservationMapper;
import org.example.completed.model.vo.ResultVO;
import org.example.completed.service.ActiveService;
import org.example.completed.service.ActivityReservationService;
import org.example.completed.service.UserService;
import org.example.completed.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动预约
 *
 * @since 2025-04-11
 */
@RestController
@RequestMapping("/activityReservation")
public class ActivityReservationController {
    @Autowired
    private ActivityReservationService activityReservationService;
    @Autowired
    private ActivityReservationMapper activityReservationMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ActiveService activeService;

    /**
     * 查询单个活动预约信息
     */
    @GetMapping("/{id}")
    public ResultVO<ActivityReservation> getById(@PathVariable String id) {
        ActivityReservation reservation = activityReservationService.getById(id);
        return reservation != null ? ResultVO.ok(reservation) : ResultVO.fail("未找到该活动预约信息");
    }

    /**
     * 查询所有活动预约信息
     */
    @GetMapping("/list")
    public ResultVO<List<ActivityReservation>> listAll() {
        List<ActivityReservation> reservations = activityReservationService.listAll();
        return ResultVO.ok(reservations);
    }

    /**
     * 新增活动预约信息
     */
    @PostMapping("/save")
    public ResultVO<Boolean> save(@RequestBody ActivityReservation reservation) {
        Active active = activeService.getById(reservation.getActivityId());
        User user = userService.getById(reservation.getUserId());
        ActivityReservation copyObject = BeanCopyUtil.copyObject(reservation, ActivityReservation.class);
        copyObject.setUserAcoount(user.getUserAcoount());
        copyObject.setName(active.getName());
        copyObject.setDescription(active.getDescription());
        copyObject.setImageUrl(active.getImageUrl());
        copyObject.setStartTime(active.getStartTime());
        copyObject.setEndTime(active.getEndTime());
        boolean result = activityReservationService.save(copyObject);
        return result ? ResultVO.ok(true) : ResultVO.fail("新增活动预约信息失败");
    }

    /**
     * 修改活动预约信息
     */
    @PostMapping("/update")
    public ResultVO<Boolean> update(@RequestBody ActivityReservation reservation) {
        boolean result = activityReservationService.updateById(reservation);
        return result ? ResultVO.ok(true) : ResultVO.fail("修改活动预约信息失败");
    }

    /**
     * 删除活动预约信息
     */
    @PostMapping("/{id}")
    public ResultVO<Boolean> remove(@PathVariable String id) {
        boolean result = activityReservationService.removeById(id);
        return result ? ResultVO.ok(true) : ResultVO.fail("删除活动预约信息失败");
    }

    /**
     * 用户报名预约接口
     */
    @PostMapping("/signUp")
    public ResultVO<String> signUp(@RequestParam String activityId, @RequestParam String userId, @RequestParam String phone) {
        // 检查 activityId 和 userId 是否已经存在 ---test
        ActivityReservation existingReservation = activityReservationMapper.selectByActivityIdAndUserId(activityId, userId);
        if (existingReservation != null) {
            return ResultVO.fail("该用户已报名此活动");
        }

        try {
            activityReservationService.signUp(activityId, userId, phone);
            return ResultVO.ok("报名成功");
        } catch (IllegalArgumentException e) {
            return ResultVO.fail(e.getMessage());
        }
    }

    /**
     * 根据 activity_id 查询所有活动预约信息
     * @param activityId 活动 ID
     * @return 包含活动预约信息的结果对象
     */
    @GetMapping("/listByActivityId")
    public ResultVO<List<ActivityReservation>> listByActivityId(@RequestParam String activityId) {
        List<ActivityReservation> reservations = activityReservationService.listByActivityId(activityId);
        return ResultVO.ok(reservations);
    }



}

